package jvm.classloader;

import java.io.*;

public class Test11 extends ClassLoader {

    private String classLoaderName;

    private String path;

    private final String fileExtension = ".class";

    public void setPath(String path) {
        this.path = path;
    }

    public Test11(String classLoaderName) {
        super(); //将系统加载器作为该类加载器的父加载器
        this.classLoaderName = classLoaderName;
    }

    public Test11(ClassLoader parent, String classLoaderName) {
        super(parent); //将parent作为该类的父加载器
        this.classLoaderName = classLoaderName;
    }

    private byte[] loadClassDate(String name) {
        InputStream is = null;
        byte[] data = null;
        ByteArrayOutputStream baos = null;
        try {
            name = name.replace(".","\\");
            //this.path + name + this.fileExtension
            is = new FileInputStream(new File( this.path + name + this.fileExtension));
            baos = new ByteArrayOutputStream();
            int ch = 0;
            while (-1 != (ch = is.read())) {
                baos.write(ch);
            }
            data = baos.toByteArray();
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                baos.close();
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return data;
    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        System.out.println("findClass invoke:" + name);
        System.out.println("classLoader name:" + this.classLoaderName);
        byte[] data = this.loadClassDate(name);
        return this.defineClass(name,data,0,data.length);
    }

    public static void test(ClassLoader classLoader) throws Exception {

    }

    public static void main(String[] args) throws Exception {
        Test11 loader1 = new Test11("loader1");
        loader1.setPath("C:\\Users\\Shinelon\\Desktop\\乱七八糟\\javastudy\\");
        Class<?> clazz = loader1.loadClass("jvm.classloader.Test1");
        System.out.println(clazz.hashCode());
        Object object = clazz.newInstance();
        System.out.println(object);

        loader1 = null;
        clazz = null;
        object = null;
        System.gc();
        System.out.println();

        loader1 = new Test11("loader1");
        loader1.setPath("C:\\Users\\Shinelon\\Desktop\\乱七八糟\\javastudy\\");
        clazz = loader1.loadClass("jvm.classloader.Test1");
        System.out.println(clazz.hashCode());
        object = clazz.newInstance();
        System.out.println(object);





        System.out.println("===================================");

        Test11 loader2 = new Test11("loader2");
        loader2.setPath("C:\\Users\\Shinelon\\Desktop\\乱七八糟\\javastudy\\");
        Class<?> clazz2 = loader2.loadClass("jvm.classloader.Test1");
        System.out.println(clazz2.hashCode());
        Object object2 = clazz2.newInstance();
        System.out.println(object2);

        System.out.println();

        Test11 loader3 = new Test11(loader1,"loader3");
        loader3.setPath("C:\\Users\\Shinelon\\Desktop\\乱七八糟\\javastudy\\");
        Class<?> clazz3 = loader3.loadClass("jvm.classloader.Test1");
        System.out.println(clazz3.hashCode());
        Object object3 = clazz3.newInstance();
        System.out.println(object3);




    }
}
